package com.ruoyi.system.service.admin;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.entity.admin.AdminCustomerDB;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.UserTypeEnum;
import com.ruoyi.common.utils.sql.MyPageUtils;
import com.ruoyi.system.mapper.admin.AdminCustomerMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collections;

@Service
@Slf4j
public class AdminCustomerService {
    @Resource
    private AdminCustomerMapper adminCustomerMapper;

    /**
     * 查询后台-客户
     *
     * @param id 后台-客户主键
     * @return 后台-客户
     */
    public AdminCustomerDB selectAdminCustomerById(Long id) {
        return adminCustomerMapper.selectById(id);
    }

    /**
     * 查询后台-客户列表
     *
     * @param adminCustomer 后台-客户
     * @return 后台-客户
     */
    public TableDataInfo selectAdminCustomerList(AdminCustomerDB adminCustomer) {
        LambdaQueryWrapper<AdminCustomerDB> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ObjectUtils.isNotEmpty(adminCustomer.getId()),AdminCustomerDB::getId, adminCustomer.getId());
        wrapper.like(ObjectUtils.isNotEmpty(adminCustomer.getUserName()),AdminCustomerDB::getUserName, adminCustomer.getUserName());
        wrapper.like(ObjectUtils.isNotEmpty(adminCustomer.getUserPhoneNumber()),AdminCustomerDB::getUserPhoneNumber, adminCustomer.getUserPhoneNumber());
        // isActive
        wrapper.eq(ObjectUtils.isNotEmpty(adminCustomer.getIsActive()),AdminCustomerDB::getIsActive, adminCustomer.getIsActive());
        // 用户状态类型 getUserType
        wrapper.eq(ObjectUtils.isNotEmpty(adminCustomer.getUserType()),AdminCustomerDB::getUserType, adminCustomer.getUserType());
        // 按照id倒序
        wrapper.orderByDesc(AdminCustomerDB::getId);

        // 分页
        Page<AdminCustomerDB> pageParam = MyPageUtils.handlePageParam();
        Page<AdminCustomerDB> communityAreaDBPage = adminCustomerMapper.selectPage(pageParam, wrapper);
        return TableDataInfo.covert(communityAreaDBPage);

    }

    /**
     * 新增后台-客户
     *
     * @param adminCustomer 后台-客户
     * @return 结果
     */
    public int insertAdminCustomer(AdminCustomerDB adminCustomer) {
        return adminCustomerMapper.insert(adminCustomer);
    }

    /**
     * 修改后台-客户
     *
     * @param adminCustomer 后台-客户
     * @return 结果
     */
    public int updateAdminCustomer(AdminCustomerDB adminCustomer) {
        return adminCustomerMapper.updateById(adminCustomer);
    }

    /**
     * 批量删除后台-客户
     *
     * @param ids 需要删除的后台-客户主键
     * @return 结果
     */
    public int deleteAdminCustomerByIds(Long[] ids) {
//        return adminCustomerMapper.deleteAdminCustomerByIds(ids);

        // 更改删除时间
        LambdaUpdateWrapper<AdminCustomerDB> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(AdminCustomerDB::getId, Arrays.asList(ids));
        updateWrapper.set(AdminCustomerDB::getDeletedAt, DateUtil.date());
        adminCustomerMapper.update(updateWrapper);

        // 使用了 @TableLogic 注解后,将这批id的删除状态置为1
        return adminCustomerMapper.deleteBatchIds(Arrays.asList(ids));

    }

    /**
     * 删除后台-客户信息
     *
     * @param id 后台-客户主键
     * @return 结果
     */
    public int deleteAdminCustomerById(Long id) {
        LambdaUpdateWrapper<AdminCustomerDB> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(AdminCustomerDB::getId, Collections.singletonList(id));
        updateWrapper.set(AdminCustomerDB::getDeletedAt, DateUtil.date());
        adminCustomerMapper.update(updateWrapper);

        // 使用了 @TableLogic 注解后,将这批id的删除状态置为1
        return adminCustomerMapper.deleteBatchIds(Collections.singletonList(id));
    }

    public int handleLevel(Long id) {
        LambdaUpdateWrapper<AdminCustomerDB> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(AdminCustomerDB::getUserType, UserTypeEnum.MEMBER.getCode());
        updateWrapper.eq(AdminCustomerDB::getId, id);
        return adminCustomerMapper.update(updateWrapper);
    }
//    public int handleLevel(Long id, String intentionAddressNo, BigDecimal amount, Integer term) {
//        LambdaUpdateWrapper<AdminCustomerDB> updateWrapper = new LambdaUpdateWrapper<>();
//        updateWrapper.set(AdminCustomerDB::getUserType, UserTypeEnum.MEMBER.getCode());
//        updateWrapper.eq(AdminCustomerDB::getId, id);
//        return adminCustomerMapper.update(updateWrapper);
//    }

    public AdminCustomerDB getUserByPhone(String phone) {
        LambdaQueryWrapper<AdminCustomerDB> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AdminCustomerDB::getUserPhoneNumber, phone);
        return adminCustomerMapper.selectOne(wrapper);
    }
}
